package marcis.jarina.hibernate.dao;

import marcis.jarina.hibernate.entities.Archive;
import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository(value = "archiveDao")
public class ArchiveDao {

    private static final Logger log = Logger.getLogger(ArchiveDao.class);

    @Autowired
    private SessionFactory sessionFactory;

    private Session session;


    public final SessionFactory getSessionFactory() {
        /*
        if (sessionFactory == null) {
            sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
        }
        */
        return sessionFactory;
    }

    public final Session getSession() {
        if (session == null || !session.isOpen()) {
            return getSessionFactory().openSession();
        }
        //return getSessionFactory().getCurrentSession();
        return session;
    }

    public void save(Archive... archives) {
        //getSession().getTransaction().begin();
        for (Archive archive : archives) {
            //getSession().saveOrUpdate(archive);
            getSession().save(archive);
        }
        //getSession().getTransaction().commit();
    }

    public List<Archive> getPagedList(int start, int maxResults) {
        try {
            String queryStr = "from Archive";
            Query query = getSession().createQuery(queryStr);
            query.setFirstResult(start);
            query.setMaxResults(maxResults);
            return (List<Archive>) query.list();
        } catch (HibernateException e) {
            log.error("error retrieving table: " + e.getMessage());
        }
        return null;
    }

    public Archive loadById(Integer id) {
        Criteria c = getSession().createCriteria(Archive.class);
        c.add(Restrictions.eq("id", id));
        return (Archive) c.uniqueResult();
    }

    public List<Archive> pagedFilter(String property, Object value, Class<Archive> classDef, int startIndex, int maxResults) {
        Criteria c = getSession().createCriteria(classDef);
        // TODO the value should not be tested to null
        if (property != null && value != null) {
            c.add(Restrictions.eq(property, value));
        }
        c.setFirstResult(startIndex);
        c.setMaxResults(maxResults);
        return (List<Archive>) c.list();
    }


    public List<Archive> filter(String property, Object value, Class<Archive> classDef) {
        Criteria c = getSession().createCriteria(classDef);
        if (property != null && value != null) {
            c.add(Restrictions.eq(property, value));
        }
        return (List<Archive>) c.list();
    }


    public Long getTotalCount() {
        Query query = getSession().createQuery("select count(*) from Archive");
        return (Long) query.uniqueResult();
    }

    public int deleteAll() {
        Query query = getSession().createQuery("delete from Archive");
        return query.executeUpdate();
    }
}
